package navigation.wzg.com.navigation.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import navigation.wzg.com.navigation.R;
import navigation.wzg.com.navigation.db.Diary;
import navigation.wzg.com.navigation.db.greendao.DiaryDao;
import navigation.wzg.com.navigation.vo.AFaApplication;

/**
 * Created by WZG on 2017/10/11.
 */

public class Badaper extends BaseAdapter {
    private Context context;
    private List<Diary> datas;
    private Activity activity;
    private DiaryDao dao;
    List<Diary> diary = new ArrayList<Diary>();

    public Badaper(Context context, List<Diary> datas) {
        dao = AFaApplication.getApplication().getDaoSession().getDiaryDao();
        datas = dao.loadAll();
        this.context = context;
        if (datas == null) {
            datas = new ArrayList<Diary>();
        }
        this.datas = datas;
    }

    public Badaper(List<Diary> diary) {
        this.diary = diary;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
            holder.time = (TextView) convertView.findViewById(R.id.item_time);
            holder.title = (TextView) convertView.findViewById(R.id.item_title);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (datas != null) {
            Diary data = datas.get(position);
            holder.time.setText(data.getTime() + "");
            holder.title.setText(data.getTitle());
        }

        return convertView;
    }

    class ViewHolder {

        TextView id, time, title;

    }

    public void setData(List<Diary> data) {
        if (data != null) {
            datas.clear();
            datas.addAll(data);
            notifyDataSetChanged();
        }
    }


}
